/**
 * Licensed under the MIT License
 *
 * Copyright (c) 2010 Benoit Saint-Maxent
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
 * this software and associated documentation files (the "Software"), to deal in
 * the Software without restriction, including without limitation the rights to
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
 * the Software, and to permit persons to whom the Software is furnished to do so,
 * subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 *
 */
package marcel.controller.command {
	import marcel.debug.*;

	import flash.events.Event;

	/**
	 * MacroCommand implements IMacroCommand
	 * Handles a stack of sub-commands and triggers their execution in a FIFO order when "execute" is called.
	 * A given ICommand instance can't be pushed twice in the stack.
	 * 
	 * @author Benoit Saint-Maxent
	 */
	public class MacroCommand extends Command implements IMacroCommand {
		
		protected var _aCommands : /*ICommand*/Array;
		
		public function MacroCommand() {
			removeAllSubCommands();
		}
		
		/**
		 * @inheritDoc
		 */
		override public function execute(event : Event = null) : void {
			super.execute(event);
			var num : uint = numSubCommands;
			if(num == 0) debugw(this + ':: No sub-command registered');
			for (var i : int = 0; i < num; i++)
				(_aCommands[i] as ICommand).execute(event);
		}
		
		/**
		 * @inheritDoc
		 */
		public function addSubCommand(command : ICommand) : Boolean {
			if(!command) return false;
			removeSubCommand(command);
			_aCommands.push(command);
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeSubCommand(command : ICommand) : Boolean {
			if(!command) return false;
			var ind : int = _aCommands.indexOf(command);
			if(ind < 0) return false;
			_aCommands.splice(ind, 1);
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeAllSubCommands() : void {
			_aCommands = new Array();
		}
		
		/**
		 * @inheritDoc
		 */
		public function removeSubCommandAt(index : uint) : Boolean {
			if(index >= numSubCommands) return false;
			_aCommands.splice(index, 1);
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function addSubCommandBefore(commandToSearch : ICommand, commandToAdd : ICommand) : Boolean {
			if(!commandToAdd || !commandToSearch) return false;
			var ind : int = _aCommands.indexOf(commandToSearch);
			if(ind < 0) return false;
			removeSubCommand(commandToAdd);
			ind = _aCommands.indexOf(commandToSearch);
			_aCommands.splice(ind, 0, commandToAdd);
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function addSubCommandAfter(commandToSearch : ICommand, commandToAdd : ICommand) : Boolean {
			if(!commandToAdd || !commandToSearch) return false;
			var ind : int = _aCommands.indexOf(commandToSearch);
			if(ind < 0) return false;
			removeSubCommand(commandToAdd);
			ind = _aCommands.indexOf(commandToSearch);
			_aCommands.splice(ind + 1, 0, commandToAdd);
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function addSubCommandAt(index : uint, command : ICommand) : Boolean {
			if(!command) return false;
			removeSubCommand(command);
			if(index > numSubCommands) return false;
			_aCommands.splice(index, 0, command);
			return true;
		}
		
		/**
		 * @inheritDoc
		 */
		public function get numSubCommands() : uint {
			return _aCommands ? _aCommands.length : 0;
		}
	}
}
